	window.onload=function(){
		//注册按钮事件
		document.getElementById("register").onclick=Register;
		
		
		
		/*
		检查用户注册信息格式
		*/
		//用户名是否格式正确且未被注册
		document.getElementById("username").onblur=function(){
			var username=this.value;
			var nameError=document.getElementById("name-error");
			if(username){
				if(!validUsername(username)){
					nameError.innerHTML="用户名只能包括大小写英文字母、数字以及下划线_，且长度在6-12之间";
					nameError.style.display="block";
				}
				else{
					var data="username="+username;
					var request=createRequest();
					if(request==null)
						alert("无法创建请求");
					else{
						request.onreadystatechange=function()
						{
						if (request.readyState==4 && request.status==200)
							{
								if(request.responseText=="exist"){
									nameError.innerHTML="用户名已被注册";
									nameError.style.display="block";
								}
								else{
									nameError.style.display="none";
								}

							}
						}
						request.open("POST","/baike/sdubaike/CheckExist",true);
						request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
						request.send(data);
						}
			}
			}
		}
		//密码是否格式正确
		document.getElementById("userpass").onblur=function(){
			var password=this.value;
			if(password)
			if(!validPass(password))
				document.getElementById("pass-error").style.display="block";
			else
				document.getElementById("pass-error").style.display="none";
		}
		//确认重复输入密码一致
		document.getElementById("repeatpass").onblur=function(){
			var repeatpass=this.value;
			if(repeatpass&&repeatpass!=document.getElementById("userpass").value)
				document.getElementById("repeat-error").style.display="block";
			else
				document.getElementById("repeat-error").style.display="none";
				
		}
		//邮箱是否格式正确且未被注册
		document.getElementById("useremail").onblur=function(){
			var email=this.value;
			var emailError=document.getElementById("email-error");
			if(email)
			if(!validEmail(email)){
				emailError.innerHTML="邮箱格式错误";
				emailError.style.display="block";
			}
				else{
					var data="email="+email;
					var request=createRequest();
					if(request==null)
						alert("无法创建请求");
					else{
						request.onreadystatechange=function()
						{
						if (request.readyState==4 && request.status==200)
							{
								if(request.responseText=="used"){
									emailError.innerHTML="邮箱已被注册";
									emailError.style.display="block";
								}
								else{
									emailError.style.display="none";
								}

							}
						}
						request.open("POST","/baike/sdubaike/EmailUsed",true);
						request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
						request.send(data);
						}
			}
	}

}
function Register(){
		var username=document.getElementById("username").value;
		var password=document.getElementById("userpass").value;
		var email=document.getElementById("useremail").value;
		var data="username="+username+"&password="+password+"&email="+email;
		var request=createRequest();
		if(request==null)
			alert("无法创建请求");
		else{
			request.onreadystatechange=function()
			{
			if (request.readyState==4 && request.status==200)
				{
					var feedBack=document.getElementById("feedBack");
					if(request.responseText=="failed"){
						feedBack.innerHTML="注册失败！服务器错误";
						feedBack.style.display="block";
					}
					else if(request.responseText=="emailfailed"){
						feedBack.innerHTML="注册失败！邮件发送失败";
						feedBack.style.display="block";
					}
					if(request.responseText=="success"){
						feedBack.innerHTML="注册成功,正在跳转到主页";
						feedBack.style.display="block";
						setTimeout(function(){
							window.location="index.html";  
						},3000)
					}
				}
			}
			request.open("POST","/baike/sdubaike/Register",true);
			request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
			request.send(data);
			}
}